<template>
  <div class="serch">
    <div class="search">
      <!-- 参数v-model="value" @search="onSearch"
          @cancel="onCancel"-->
      <van-search
        v-model="value"
        show-action
        background="#3296fa"
        shape="round"
        placeholder="请输入搜索关键词"
        @search="onSearch"
        @input="input"
      >
        <template #action>
          <div class="clean" @click="onCancel">
            取消
          </div>
        </template>
      </van-search>
    </div>
    <div v-if="bol" class="history">
      <div class="c1">
        <div class="f1">搜索记录</div>
        <div class="f2">
          <van-tag
            v-for="(item, index) in searchHistory"
            :key="index"
            color="#eece03"
            round
            >{{ item }}</van-tag
          >
        </div>
      </div>
      <div class="c1">
        <div class="f1">历史搜索</div>
        <div class="f2"><van-tag>1111</van-tag></div>
      </div>
    </div>
    <!-- 关键词 -->
    <div v-else>
      <div
        class="keyword"
        v-for="(item, index) in keywordlist"
        :key="index"
        @click="search(index)"
      >
        <div class="c1">
          <i class="iconfont">&#xe647;</i>
        </div>
        <div class="c2 van-ellipsis" v-html="item"></div>
      </div>
    </div>
  </div>
</template>

<script>
import { suggestion, searcHstories } from '@/api/search.js'
import { setLocal, getLocal } from '@/utils/local.js'
export default {
  neme: 'search',
  data () {
    return {
      bol: true,
      value: '', // 输入框的值
      keywordlist: [], // 搜索匹配信息
      options: [],
      searchHistory: JSON.parse(getLocal('search')) || [], // 搜索历史
      historysearch: [] // 历史搜索
    }
  },
  async created () {
    //   获取历史搜索信息
    const res = await searcHstories()

    // 获取历史搜索
    this.historysearch = res.data.keywords
  },
  //   监听value是否有值
  methods: {
    // 点击取消事件
    onCancel () {
      // 清除文本值
      this.value = ''
      this.$router.push('/home/question')
    },
    async input () {
      // 每次输入前让数组为空
      this.keywordlist = []
      //   判断是否输入类容
      if (this.value.trim() !== '') {
        // 让历史搜索隐藏
        this.bol = false
        const res = await suggestion({ q: this.value })
        res.data.options.forEach(item => {
          // 分割
          item = item.split(this.value)
          // 拼接
          item = item.join(`<span  style='color:#23abf2'>${this.value}</span>`)
          this.keywordlist.push(item)
        })
        // 新定义数组
        this.options = res.data.options
      }
    },
    // 按确认搜索
    onSearch () {
      // 判断是否输入内容
      // 清除文本值
      if (this.value !== '') {
        this.searchHistory.unshift(this.value)
      }
      // 去重
      this.searchHistory = [...new Set(this.searchHistory)]
      // 保留五个结果
      this.searchHistory = this.searchHistory.slice(0, 5)
      setLocal('search', JSON.stringify(this.searchHistory))
      this.value = ''
      this.bol = true
    },
    // 点击标签搜索
    search (index) {
      this.searchHistory.unshift(this.options[index])
      // 去重
      this.searchHistory = [...new Set(this.searchHistory)]
      // 保留五个结果
      this.searchHistory = this.searchHistory.slice(0, 5)
      setLocal('search', JSON.stringify(this.searchHistory))
      this.value = ''
      this.bol = true
    }
  }
}
</script>

<style lang="less">
.serch {
  .search {
    .clean {
      color: #fff;
    }
  }
  .keyword {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 6px 15px;
    border-bottom: 1px solid #ccc;
    font-family: MicrosoftYaHei;
    font-size: 14px;
    font-weight: normal;
    font-stretch: normal;
    line-height: 36px;
    letter-spacing: 1px;
    .c1 {
      margin-right: 15px;
      .iconfont {
        font-size: 18px;
        color: #c9c9c9;
      }
    }
    .c2 {
      flex: 1;
    }
  }
  .history {
    padding: @p15;
    .c1 {
      .f1 {
        padding-left: 20px;
        padding-bottom: 10px;
        font-family: MicrosoftYaHei;
        font-size: 16px;
        font-weight: normal;
        font-stretch: normal;
        line-height: 18px;
        letter-spacing: 2px;
        color: #333333;
        border-bottom: 1px solid #cccccc;
      }
      .f2 {
        margin-top: 20px;
        margin-bottom: 20px;
        display: flex;
        justify-content: space-between;
        flex-wrap: wrap;
        .van-tag {
          margin: 10px;
          padding: 5px;
        }
      }
    }
  }
}
</style>
